import java.util.Arrays;

public class MyStack<E> {
    public E[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = (E[])new Object[2];
    }

    public void push(E val) {
        if (isFull()) {
            elem = Arrays.copyOf(elem, 2 * elem.length);
        }
        this.elem[this.usedSize++] = val;
    }

    public boolean isFull() {
        return elem.length == usedSize;
    }

    public E pop() {
        if (empty()) {
            throw new RuntimeException("栈为空");
        }
        E ret = this.elem[usedSize-1];
        elem[usedSize-1] = null;
        usedSize--;
        return ret;
    }

    public boolean empty() {
        return usedSize == 0;
    }

    public E peek() {
        if (empty()) {
            throw new RuntimeException("栈为空");
        }
        return elem[usedSize-1];
    }

    public int getUsedSize() {
        return this.usedSize;
    }
}
